<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">
            <!-- 放置表单 -->
            <el-form ref="userForm" :model="formData" :rules="rules" label-width="120px" style="margin-left: 120px; margin-top:30px">
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="formData.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:" prop="password2">
                <el-input v-model="formData.password2" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="saveUserDetailById">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <user-info />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <job-info />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getUserDetailById, saveUserDetailById } from '@/api/user'
import userInfo from './user-info.vue'
import JobInfo from './job-info.vue'
export default {
  components: { userInfo, JobInfo },
  data() {
    return {
      formData: {
        username: '',
        // 这里如果绑定 password
        // 会有将加密密文当做密码保存的危险
        // 所以另外创建一个临时变量用来跟用户的输入绑定
        password2: ''
      },
      rules: {
        username: [
          { required: true, message: '当前项不能为空', trigger: 'blur' },
          { min: 2, max: 16, message: '名字长度在 2 到 16 之间', trigger: 'blur' }
        ],
        password2: [
          { required: true, message: '当前项不能为空', trigger: 'blur' },
          { min: 6, max: 16, message: '名字长度在 6 到 16 之间', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getUserDetailById(this.$route.params.id)
  },
  methods: {
    async getUserDetailById(id) {
      // 获取员工个人信息
      this.formData = await getUserDetailById(id)
    },
    async saveUserDetailById() {
      // 1 校验表单
      await this.$refs.userForm.validate()
      // 2 发送请求
      await saveUserDetailById({ ...this.formData, password: this.password2 })
      // 3 提示成功
      this.$message.success('更新成功')
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
